package leetcode_二叉树;

import general_class.TreeNode;

/**
 * 将有序数组转换为二叉搜索树
 * 算法：递归
 *
 * @author yezh
 * @date 2024/1/21 18:01
 */
public class _108 {

    public TreeNode sortedArrayToBST(int[] nums) {
        return recursion(nums, 0, nums.length - 1);
    }

    TreeNode recursion(int[] nums, int l, int r) {
        if (l < r) return null;
        if (l == r) return new TreeNode(nums[l]);
        int mid = l + (r - l) / 2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = recursion(nums, l, mid - 1);
        node.right = recursion(nums, mid + 1, r);
        return node;
    }

}
